package com.zxmzy.scaffold.core.ext

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

/**
 * @author zhangxiaoming
 * @date 2023/7/25
 * @description
 */
/**
 * 对像类型转换
 */
inline fun <reified T> Any.saveAs(): T {
    return this as T
}

@Suppress("UNCHECKED_CAST")
fun <T> Any.saveAsUnChecked(): T {
    return this as T
}

inline fun <reified T> Any.isEqualType(): Boolean {
    return this is T
}

fun Any.toJson(): String {
    val gson = Gson()
    return gson.toJson(this)
}

inline fun <reified T> String.fromJson(): T {
    val gson = Gson()
    return gson.fromJson(this, T::class.java)
}

inline fun <reified T> String.toBeanList(): List<T> {
    val gson = Gson()
    val type = object : TypeToken<List<T>>() {}.type
    return gson.fromJson(this, type)
}